package com.pubinfo.passbook.engine.process;

import com.pubinfo.passbook.engine.Computor;
import com.pubinfo.passbook.engine.cache.ComputeCache;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/**
 * 参数处理器
 * <p>
 * 在计算链运行时如果时占位符参数会根据参数处理器和上下文自动生成对应的真实值.
 * <p>
 * T: 所需要的上下文数据类型
 * R: 返回值
 */
public abstract class ParamenterProcessor<T, R> {

    protected String placeholderName;

    /**
     * 计算链缓存
     */
    @Setter
    protected ComputeCache cache;

    /**
     * 获得参数处理器名称
     *
     * @return
     */
    public String getName() {
        return StringUtils.EMPTY;
    }

    /**
     * 得到真实值
     *
     * @param data
     * @return
     */
    public abstract R get(T data);

    /**
     * 返回值
     * {@link com.pubinfo.passbook.engine.constants.ComputeResultType}
     *
     * @return
     */
    public abstract String getResultType();

    /**
     * 计算链类型
     *
     * @return
     */
    public abstract String getChainType();

    public abstract String getType();

    /*
     * 设置占位符名称
     */
    public void setPlaceHolderName(String name) {
        placeholderName = name;
    }

    public String getPlaceHolderName() {
        return placeholderName;
    }


    @Override
    public String toString() {
        return "参数名称(计算单元占位符): " + getPlaceHolderName() + "\n" + "计算链类型: " + getChainType() + "\n" + "计算结果类型: " + getResultType() + "\n";
    }

    /**
     * 将计算单元执行信息压入全局计算缓存中.
     */
    protected void outputComputeMessage(String name, String result) {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(result)) {
            return;
        }

        String msg = name + ": " + result;
        cache.pushExecuteMessage(msg);
    }

    protected void outputComputeMessage(String result) {
        outputComputeMessage(getName(), result);
    }
}
